<?php
namespace app\shell;

use share\console\CConsole;
use share\console\CLIRunner;
use share\service\MQService;
use share\service\SendCodeHistoryService;
use share\service\SendCodeService;
use share\utils\Helper;

class CSendCode extends CLIRunner{

    public function send_email_action(){
        $email = '375562657@qq.com'; 
        $location = 'cn';   
        $sendCodeService = SendCodeService::instance(
            SendCodeService::SEND_CODE_SCENE_RESET_PASS,
            SendCodeService::SEND_CODE_SEND_TYPE_OF_EMAIL,
            $email
        );    
        $code = Helper::getRandomStr(6, Helper::RANDOM_STR_TYPE_NUM);
        $sendCodeService->setCode($code);
        $sendParam = [
            'vendor' => "ENZ",
            'package' => 'com.ml.yunmonitordtest',
            'language' => $location == 'cn' ? 'zh-cn' : 'en-us',
            'location' => $location,
            'replace' => [
                'code' => $code,
                'email' => $email,
            ]
        ];   
        $sendCodeService->setParam($sendParam);
        $sendCodeService->addToQueue();
        return $sendParam;


        // SendCodeService::sendEmail(
        //     [
        //         'code' => '123456',
        //         'scene' => SendCodeService::SEND_CODE_SCENE_REGISTER,
        //         'receiver' => 'razeforoosh@gmail.com',
        //         'vendor' => 'ENZ',
        //         'package' => 'com.antsvision.seeeasy',
        //         'language' => 'zh-cn',
        //         'location' => 'cn',
        //         'replace' => [
        //             'code' => '123456',
        //             'email' => 'master'
        //         ],
        //     ]
        // );
    }

    public function send_sms_action(){

        SendCodeService::sendSMS(
            [
                'code' => '963217',
                'scene' => 'reset_pswd',
                'receiver' => '17688794855',
                'vendor' => 'ENZ',
                'package' => 'com.ml.yunmonitordtest',
                'language' => 'zh-cn',
                'location' => 'cn',
                "sendType" => "phone",
                'ext' => [
                    'phoneCode' => 86,
                    'isoCode' => ''
                ],
                // 'replace' => [
                //     'code' => '123456',
                //     'email' => 'master'
                // ],
            ]
        );
    }

    public function test_delay_action(){
        
        MQService::pushDelay([
            'service' => 'share\\service\\SendCodeService@sendSMS',
            'params' => [
                "language" => "zh-cn",
                "location" => "cn",
                "scene" => "register",
                "sendType" => "phone",
                "receiver" => "17688794855",
                "code" => "6789",
            ]            
        ],20000, MQService::CHANNEL_OF_ACCOUNT);
    }

    /**
     * 删除验证码发送记录
     */
    public function del_send_code_history_action($day = 5){
        $day = intval($day);
        if(!$day){
            $day =5;
        }
        if($day < 1){
            $day = 3;
        }
        $time = strtotime("-{$day} days");
        SendCodeHistoryService::delByFilter([
            'created' =>  ['<',$time],
        ]);
        CConsole::print_line("deleted all before ".date("Y-m-d H:i:s",$time));
    }

}